package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.entity.WorkCompanyInfo;
import com.lst.api.mapper.WorkCompanyInfoMapper;
import com.lst.api.service.IWorkCompanyInfoService;
import com.lst.api.vo.WorkCompanyInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 工作单位信息业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:38
 * @version: V1.0
 */
@Service
public class WorkCompanyInfoService extends BaseService<WorkCompanyInfoMapper, WorkCompanyInfo> implements IWorkCompanyInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, WorkCompanyInfoVo workCompanyInfoVo) {
        if (workCompanyInfoVo == null || workCompanyInfoVo.isAllFieldNull()) {
            return false;
        }
        workCompanyInfoVo.check();
        WorkCompanyInfo workCompanyInfo = new WorkCompanyInfo(userId, applyNo, workCompanyInfoVo);
        QueryWrapper<WorkCompanyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(WorkCompanyInfo.APPLY_NO, applyNo)
                .eq(WorkCompanyInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(workCompanyInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<WorkCompanyInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(WorkCompanyInfo.APPLY_NO, applyNo)
                    .eq(WorkCompanyInfo.PROMO_USER_ID, userId)
                    .set(WorkCompanyInfo.UPDATE_AT, new Date())
                    .set(WorkCompanyInfo.COMPANY_NAME, workCompanyInfo.getCompanyName())
                    .set(WorkCompanyInfo.DEPARTMENT, workCompanyInfo.getDepartment())
                    .set(WorkCompanyInfo.POST, workCompanyInfo.getPost())
                    .set(WorkCompanyInfo.ADDRESS_PROVINCE_CODE, workCompanyInfo.getAddressProvinceCode())
                    .set(WorkCompanyInfo.ADDRESS_CITY_CODE, workCompanyInfo.getAddressCityCode())
                    .set(WorkCompanyInfo.ADDRESS_AREA_CODE, workCompanyInfo.getAddressAreaCode())
                    .set(WorkCompanyInfo.ADDRESS_DETAILS, workCompanyInfo.getAddressDetails())
                    .set(WorkCompanyInfo.COMPANY_PHONE, workCompanyInfo.getCompanyPhone())
                    .set(WorkCompanyInfo.INDUSTRY_TYPE_ID, workCompanyInfo.getIndustryTypeId());
            return super.update(updateWrapper);
        }
        return true;
    }

    @Override
    public WorkCompanyInfo getByApplyNo(String applyNo) {
        QueryWrapper<WorkCompanyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(WorkCompanyInfo.APPLY_NO, applyNo);
        return super.getOne(queryWrapper);
    }
}